package com.viewlibrary.loadview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 绘制圆、三角形、正方形，并根据当前的形状确定下一个要绘制的形状
 */
public class ShapeView extends View {
    private Shape mCurrentShpae=Shape.Square;
    private Paint mPaint;

    public ShapeView(Context context) {
        this(context,null);
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint=new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width =MeasureSpec.getSize(widthMeasureSpec);
        int height=MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(Math.min(width,height),Math.min(width,height));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        switch (mCurrentShpae){
            case Circle:
                //是圆就绘制正方形
                mPaint.setColor(Color.RED);
                canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
                break;
            case Square:
                //是正方形就绘制三角形
                mPaint.setColor(Color.BLUE);
                Path path=new Path();
                path.moveTo(getWidth()/2,0);
                path.lineTo(0, (float) (getWidth()/2*Math.sqrt(3)));
                path.lineTo(getWidth(), (float) (getWidth()/2*Math.sqrt(3)));
                path.close();
                canvas.drawPath(path,mPaint);
                break;
            case Triangle:
                //是三角形就绘制圆
                mPaint.setColor(Color.YELLOW);
                canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2,mPaint);
                break;
        }
    }

    enum Shape{
        Circle,Square,Triangle
    }

    public void exChangeShape(){
        switch (mCurrentShpae){
            case Circle:
                mCurrentShpae=Shape.Square;
                break;
            case Square:
                mCurrentShpae=Shape.Triangle;
                break;
            case Triangle:
                mCurrentShpae=Shape.Circle;
                break;
        }
        invalidate();
    }

    public Shape getCurrentShape(){
        return mCurrentShpae;
    }
}
